package com.ustc.hewei.snowflakemodule.segment.entity;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author hewei
 * @version 1.0
 * @description: 存放号段包装类的Buffer
 * @date 2022/11/16 15:18
 */

public class SegmentBuffer {

    private String bizTag;
    private final AtomicBoolean isUpdate;
    private volatile boolean initRes;
    private volatile int currentPos;
    private volatile boolean nextReady;
    private final Segment[] segments;
    private volatile int step;
    private volatile int minStep;
    private volatile long updateTime;
    private final ReadWriteLock lock;

    public int getNextPos() {
        return (currentPos + 1) % 2;
    }

    public void switchPos() {
        currentPos = getNextPos();
    }

    public Segment getNextSegment() {
        return segments[getNextPos()];
    }

    public Lock getRLock() {
        return lock.readLock();
    }

    public Lock getWLock() {
        return lock.writeLock();
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public int getMinStep() {
        return minStep;
    }

    public void setMinStep(int minStep) {
        this.minStep = minStep;
    }

    public SegmentBuffer() {
        segments = new Segment[]{new Segment(this), new Segment(this)};
        isUpdate = new AtomicBoolean(false);
        currentPos = 0;
        initRes = false;
        lock = new ReentrantReadWriteLock();
    }

    public Segment getCurrentSegment() {
        return segments[currentPos];
    }



    public void setBizTag(String bizTag) {
        this.bizTag = bizTag;
    }

    public void setInitRes(boolean initRes) {
        this.initRes = initRes;
    }

    public void setNextReady(boolean nextReady) {
        this.nextReady = nextReady;
    }

    public String getBizTag() {
        return bizTag;
    }

    public AtomicBoolean getIsUpdate() {
        return isUpdate;
    }

    public boolean getInitRes() {
        return initRes;
    }

    public int getCurrentPos() {
        return currentPos;
    }

    public boolean getNextReady() {
        return nextReady;
    }

    @Override
    public String toString() {
        return "SegmentBuffer{" +
                "bizTag='" + bizTag + '\'' +
                ", isUpdate=" + isUpdate +
                ", initRes=" + initRes +
                ", currentPos=" + currentPos +
                ", nextReady=" + nextReady +
                ", segments=" + Arrays.toString(segments) +
                ", step=" + step +
                ", minStep=" + minStep +
                ", updateTime=" + updateTime +
                '}';
    }
}
